﻿namespace UnityEditor
{
    using System;
    using System.Collections;
    using UnityEngine;

    [CustomEditor(typeof(Cubemap))]
    internal class CubemapTextureInspector : Editor
    {
        private Texture2D[] images;
        private static string[] kSizes = new string[] { "16", "32", "64", "128", "256", "512", "1024", "2048" };
        private static int[] kSizesValues = new int[] { 0x10, 0x20, 0x40, 0x80, 0x100, 0x200, 0x400, 0x800 };
        private const int kTextureSize = 0x40;
        private Material m_Material;
        private Mesh m_Mesh;
        private PreviewRenderUtility m_PreviewUtility;
        public Vector2 previewDir = new Vector2(0f, 0f);

        public override bool HasPreviewGUI()
        {
            return (base.target != null);
        }

        private void InitPreview()
        {
            if (this.m_PreviewUtility == null)
            {
                this.m_PreviewUtility = new PreviewRenderUtility();
                this.m_PreviewUtility.m_CameraFieldOfView = 30f;
                this.m_Material = new Material("Shader \"Hidden/CubemapInspector\" {\n                        Properties {\n\t                        _MainTex (\"\", Cube) = \"\" { TexGen CubeReflect }\n                        }\n                        SubShader {\n                            Tags { \"ForceSupported\" = \"True\" } \n\t                        Pass { SetTexture[_MainTex] { matrix [_CubemapRotation] combine texture } }\n                        }\n                        Fallback Off\n                        }");
                this.m_Material.hideFlags = HideFlags.HideAndDontSave;
                this.m_Material.shader.hideFlags = HideFlags.HideAndDontSave;
                this.m_Material.mainTexture = base.target as Texture;
            }
            if (this.m_Mesh == null)
            {
                GameObject obj2 = (GameObject) EditorGUIUtility.LoadRequired("Previews/PreviewMaterials.fbx");
                obj2.SetActive(false);
                IEnumerator enumerator = obj2.transform.GetEnumerator();
                try
                {
                    while (enumerator.MoveNext())
                    {
                        Transform current = (Transform) enumerator.Current;
                        if (current.name == "sphere")
                        {
                            this.m_Mesh = ((MeshFilter) current.GetComponent(typeof(MeshFilter))).sharedMesh;
                        }
                    }
                }
                finally
                {
                    IDisposable disposable = enumerator as IDisposable;
                    if (disposable == null)
                    {
                    }
                    disposable.Dispose();
                }
            }
        }

        private void InitTexturesFromCubemap()
        {
            Cubemap target = base.target as Cubemap;
            if (target != null)
            {
                if (this.images == null)
                {
                    this.images = new Texture2D[6];
                }
                for (int i = 0; i < 6; i++)
                {
                    if ((this.images[i] != null) && !EditorUtility.IsPersistent(this.images[i]))
                    {
                        UnityEngine.Object.DestroyImmediate(this.images[i]);
                    }
                    if (TextureUtil.GetSourceTexture(target, (CubemapFace) i) != null)
                    {
                        this.images[i] = TextureUtil.GetSourceTexture(target, (CubemapFace) i);
                    }
                    else
                    {
                        this.images[i] = new Texture2D(0x40, 0x40, TextureFormat.ARGB32, false);
                        this.images[i].hideFlags = HideFlags.HideAndDontSave;
                        TextureUtil.CopyCubemapFaceIntoTexture(target, (CubemapFace) i, this.images[i]);
                    }
                }
            }
        }

        public static UnityEngine.Object ObjectField(string label, UnityEngine.Object obj, System.Type objType, bool allowSceneObjects, params GUILayoutOption[] options)
        {
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            GUILayoutOption[] optionArray1 = new GUILayoutOption[] { GUILayout.ExpandWidth(false) };
            GUI.Label(GUILayoutUtility.GetRect(EditorGUIUtility.labelWidth, 32f, EditorStyles.label, optionArray1), label, EditorStyles.label);
            GUILayoutOption[] optionArray2 = new GUILayoutOption[] { GUILayout.Width(64f) };
            UnityEngine.Object obj2 = EditorGUI.ObjectField(GUILayoutUtility.GetAspectRect(1f, EditorStyles.objectField, optionArray2), obj, objType, allowSceneObjects);
            GUILayout.EndHorizontal();
            return obj2;
        }

        internal override void OnAssetStoreInspectorGUI()
        {
            this.OnInspectorGUI();
        }

        public void OnDisable()
        {
            if (this.images != null)
            {
                for (int i = 0; i < 6; i++)
                {
                    if (!EditorUtility.IsPersistent(this.images[i]))
                    {
                        UnityEngine.Object.DestroyImmediate(this.images[i]);
                    }
                }
            }
            this.images = null;
            if (this.m_PreviewUtility != null)
            {
                this.m_PreviewUtility.Cleanup();
                this.m_PreviewUtility = null;
            }
            if (this.m_Material != null)
            {
                UnityEngine.Object.DestroyImmediate(this.m_Material.shader, true);
                UnityEngine.Object.DestroyImmediate(this.m_Material, true);
                this.m_Material = null;
            }
        }

        public void OnEnable()
        {
            this.InitTexturesFromCubemap();
        }

        public override void OnInspectorGUI()
        {
            if (this.images == null)
            {
                this.InitTexturesFromCubemap();
            }
            EditorGUIUtility.LookLikeControls(50f);
            Cubemap target = base.target as Cubemap;
            GUILayout.BeginVertical(new GUILayoutOption[0]);
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            this.ShowFace("Right\n(+X)", CubemapFace.PositiveX);
            this.ShowFace("Left\n(-X)", CubemapFace.NegativeX);
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            this.ShowFace("Top\n(+Y)", CubemapFace.PositiveY);
            this.ShowFace("Bottom\n(-Y)", CubemapFace.NegativeY);
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            this.ShowFace("Front\n(+Z)", CubemapFace.PositiveZ);
            this.ShowFace("Back\n(-Z)", CubemapFace.NegativeZ);
            GUILayout.EndHorizontal();
            GUILayout.EndVertical();
            GUI.changed = false;
            EditorGUIUtility.LookLikeControls();
            EditorGUILayout.Space();
            int gLWidth = TextureUtil.GetGLWidth(target);
            gLWidth = EditorGUILayout.IntPopup("Face size", gLWidth, kSizes, kSizesValues, new GUILayoutOption[0]);
            int num2 = TextureUtil.CountMipmaps(target);
            bool useMipmap = EditorGUILayout.Toggle("MipMaps", num2 > 1, new GUILayoutOption[0]);
            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal(new GUILayoutOption[0]);
            bool linearSampled = TextureUtil.GetLinearSampled(target);
            linearSampled = EditorGUILayout.Toggle("Linear", linearSampled, new GUILayoutOption[0]);
            bool flag3 = TextureUtil.IsCubemapReadable(target);
            flag3 = EditorGUILayout.Toggle("Readable", flag3, new GUILayoutOption[0]);
            if (GUI.changed)
            {
                if (TextureUtil.ReformatCubemap(ref target, gLWidth, gLWidth, target.format, useMipmap, linearSampled))
                {
                    this.InitTexturesFromCubemap();
                }
                TextureUtil.MarkCubemapReadable(target, flag3);
                target.Apply();
            }
        }

        public override void OnPreviewGUI(Rect r, GUIStyle background)
        {
            if (!ShaderUtil.hardwareSupportsRectRenderTexture)
            {
                if (Event.current.type == EventType.Repaint)
                {
                    EditorGUI.DropShadowLabel(new Rect(r.x, r.y, r.width, 40f), "Cubemap preview requires\nrender texture support");
                }
            }
            else
            {
                this.InitPreview();
                this.previewDir = PreviewGUI.Drag2D(this.previewDir, r);
                if (Event.current.type == EventType.Repaint)
                {
                    this.m_PreviewUtility.BeginPreview(r, background);
                    bool fog = RenderSettings.fog;
                    Unsupported.SetRenderSettingsUseFogNoDirty(false);
                    this.m_PreviewUtility.m_Camera.transform.position = (Vector3) (-Vector3.forward * 3f);
                    this.m_PreviewUtility.m_Camera.transform.rotation = Quaternion.identity;
                    Quaternion q = Quaternion.Euler(this.previewDir.y, 0f, 0f) * Quaternion.Euler(0f, this.previewDir.x, 0f);
                    this.m_Material.SetMatrix("_CubemapRotation", Matrix4x4.TRS(Vector3.zero, q, Vector3.one));
                    this.m_PreviewUtility.DrawMesh(this.m_Mesh, Vector3.zero, q, this.m_Material, 0);
                    this.m_PreviewUtility.m_Camera.Render();
                    Unsupported.SetRenderSettingsUseFogNoDirty(fog);
                    Texture image = this.m_PreviewUtility.EndPreview();
                    GUI.DrawTexture(r, image, ScaleMode.StretchToFill, false);
                }
            }
        }

        public override void OnPreviewSettings()
        {
            if (ShaderUtil.hardwareSupportsRectRenderTexture)
            {
                GUI.enabled = true;
                this.InitPreview();
            }
        }

        private void ShowFace(string label, CubemapFace face)
        {
            Cubemap target = base.target as Cubemap;
            int index = (int) face;
            GUI.changed = false;
            Texture2D textureRef = (Texture2D) ObjectField(label, this.images[index], typeof(Texture2D), false, new GUILayoutOption[0]);
            if (GUI.changed)
            {
                TextureUtil.CopyTextureIntoCubemapFace(textureRef, target, face);
                this.images[index] = textureRef;
            }
        }
    }
}

